package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.RoleMapper;
import com.kkb.mapper.RoleResourceMapper;
import com.kkb.pojo.Role;
import com.kkb.pojo.RoleExample;
import com.kkb.pojo.RoleResource;
import com.kkb.pojo.RoleResourceExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Transactional(readOnly = true)
    public PageInfo<Role> selectRoleInPage(Integer index, Integer size, String name){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (name!=null){
            criteria.andRoleNameLike("%"+name+"%");
        }
        criteria.andRoleStateEqualTo(0);
        PageHelper.startPage(index,size);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        return new PageInfo<>(roles);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public boolean insertRole(Role role,List<Integer> list){
        int result = roleMapper.insertSelective(role);
        int roleId = role.getRoleId();
        int re =0;
        for (int i=0;i<list.size();i++) {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(list.get(i));
            roleResourceMapper.insertSelective(roleResource);
            re++;
        }
        return (result>0 && re>0);
    }

    @Transactional(readOnly = true)
    public Role selectRoleById(Integer id){
        return roleMapper.selectRoleJoinResource(id);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateRole(Role role, List<Integer> list){
        int i = roleMapper.updateByPrimaryKeySelective(role);
        int insert = 0;
        RoleResourceExample roleResourceExample = new RoleResourceExample();
        roleResourceExample.createCriteria().andRoleIdEqualTo(role.getRoleId());
        int i1 = roleResourceMapper.deleteByExample(roleResourceExample);
        for (Integer resourceId:list) {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(role.getRoleId());
            roleResource.setResourceId(resourceId);
            insert = roleResourceMapper.insert(roleResource);
        }
        return i+insert;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int logicalDeleteRole(Integer id){
        Role role = new Role();
        role.setRoleId(id);
        role.setRoleState(1);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteRoleList(List<Integer> list){
        int i1 =0;
        for (int i = 0; i <list.size() ; i++) {
            Role role = new Role();
            role.setRoleId(list.get(i));
            role.setRoleState(1);
            i1 += roleMapper.updateByPrimaryKeySelective(role);
        }
        return i1;
    }

    @Transactional(readOnly = true)
    public List<Role> selectRoleList(){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andRoleStateEqualTo(0);
        return roleMapper.selectByExample(roleExample);
    }
}
